<?php
// 1.根据 id 修改 p_name,p_price,p_unit,p_image_url 信息
function updateById($id, $name, $price, $unit, $imageUrl) {
    $url = "localhost:3306";
    $user = "root";
    $password = "";// 如果没有密码, 就是空字符串
    $database = "php";
    $conn = mysqli_connect($url, $user, $password, $database);
    mysqli_query($conn, "set names utf8");

    $sql = "update product "
        ." set "
        ." p_name = '$name', "
        ." p_price = $price, "
        ." p_unit = '$unit', "
        ." p_image_url = '$imageUrl' "
        ." where p_id = $id "
        ;
    // 执行sql语句
    $result = mysqli_query($conn, $sql);
    if($result){
        echo "<div style='color:green;' >修改成功</div>";
    } else {
        $msg = mysqli_error($conn);
        echo "<div style='color:red;' >修改失败:{$msg}</div>";
    }
    mysqli_close($conn);
}

// 测试
//updateById(4, "考拉", 66.6, "盒", "/day03/images/Koala.jpg");

// 2.根据 id 删除商品信息
function deleteById($id) {
    $url = "localhost:3306";
    $user = "root";
    $password = "";// 如果没有密码, 就是空字符串
    $database = "php";
    $conn = mysqli_connect($url, $user, $password, $database);
    mysqli_query($conn, "set names utf8");

    $sql = "delete from product where p_id = $id";
    // 执行sql语句
    $result = mysqli_query($conn, $sql);
    if($result){
        echo "<div style='color:green;' >删除成功</div>";
    } else {
        $msg = mysqli_error($conn);
        echo "<div style='color:red;' >删除失败:{$msg}</div>";
    }
    mysqli_close($conn);
}

// deleteById(4);
require_once("./DButil.php");

function deleteById2($id) {
    $conn = getConn();

    $sql = "delete from product where p_id = $id";
    // 执行sql语句
    $result = mysqli_query($conn, $sql);
    if($result){
        echo "<div style='color:green;' >删除成功</div>";
    } else {
        $msg = mysqli_error($conn);
        echo "<div style='color:red;' >删除失败:{$msg}</div>";
    }
    mysqli_close($conn);
}

deleteById2(4);
?>